package hhpoll.service.ws;

import org.apache.log4j.Logger;

public class HTTPBasicAuthHelper {

    private static Logger log = Logger.getLogger(HTTPBasicAuthHelper.class);

    /*
    public static RequestContext getRequestContext() {
        MessageContext context = MessageContext.getCurrentMessageContext();
        HttpServletRequest req = (HttpServletRequest) context.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
        RequestContext reqContext = RequestContext.getContext();

        String authHeader = req.getHeader("Authorization");
        log.info("Authorization="+authHeader);
        if (authHeader != null) {
            StringTokenizer st = new StringTokenizer(authHeader);
            if (st.hasMoreTokens()) {
                String token = st.nextToken();
                log.info("Token="+token);

                // We only handle HTTP Basic authentication
                if (token.equalsIgnoreCase("Basic")) {
                    String credentials = st.nextToken();
                    log.info("credentials="+credentials);

                    String userPass = new String(Base64.decodeBase64(credentials.getBytes()));
                    log.info("userPass="+userPass);

                    // The decoded string is in the form "userID:password".
                    int p = userPass.indexOf(":");
                    if (p != -1) {
                        String userID = userPass.substring(0, p);
                        String password = userPass.substring(p+1);
                        reqContext.setUsername(userID);
                        reqContext.setPassword(password);
                    }
                }
            }
        }

        return reqContext;
    }
    */
}
